﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BMSQ.DBUtility;//Please add references
namespace BMSQ.DAL
{
    /// <summary>
    /// 数据访问类:Transaction_records
    /// </summary>
    public partial class Transaction_records
    {
        public Transaction_records()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string TR_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Transaction_records");
            strSql.Append(" where TR_ID=@TR_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@TR_ID", SqlDbType.NVarChar,50)			};
            parameters[0].Value = TR_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Transaction_records model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Transaction_records(");
            strSql.Append("TR_ID,User_ID,Tr_code,Tr_Money,Tr_State,TradeName,TradeNo,TradeStatus,Created_Date,Update_Date,OrderNumber)");
            strSql.Append(" values (");
            strSql.Append("@TR_ID,@User_ID,@Tr_code,@Tr_Money,@Tr_State,@TradeName,@TradeNo,@TradeStatus,@Created_Date,@Update_Date,@OrderNumber)");
            SqlParameter[] parameters = {
					new SqlParameter("@TR_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Tr_code", SqlDbType.NVarChar,50),
					new SqlParameter("@Tr_Money", SqlDbType.Decimal,9),
					new SqlParameter("@Tr_State", SqlDbType.Int,4),
					new SqlParameter("@TradeName", SqlDbType.NVarChar,100),
					new SqlParameter("@TradeNo", SqlDbType.NVarChar,50),
					new SqlParameter("@TradeStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@Created_Date", SqlDbType.DateTime),
					new SqlParameter("@Update_Date", SqlDbType.DateTime),
					new SqlParameter("@OrderNumber", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.TR_ID;
            parameters[1].Value = model.User_ID;
            parameters[2].Value = model.Tr_code;
            parameters[3].Value = model.Tr_Money;
            parameters[4].Value = model.Tr_State;
            parameters[5].Value = model.TradeName;
            parameters[6].Value = model.TradeNo;
            parameters[7].Value = model.TradeStatus;
            parameters[8].Value = model.Created_Date;
            parameters[9].Value = model.Update_Date;
            parameters[10].Value = model.OrderNumber;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Transaction_records model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Transaction_records set ");
            strSql.Append("User_ID=@User_ID,");
            strSql.Append("Tr_code=@Tr_code,");
            strSql.Append("Tr_Money=@Tr_Money,");
            strSql.Append("Tr_State=@Tr_State,");
            strSql.Append("TradeName=@TradeName,");
            strSql.Append("TradeNo=@TradeNo,");
            strSql.Append("TradeStatus=@TradeStatus,");
            strSql.Append("Created_Date=@Created_Date,");
            strSql.Append("Update_Date=@Update_Date,");
            strSql.Append("OrderNumber=@OrderNumber");
            strSql.Append(" where TR_ID=@TR_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@User_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Tr_code", SqlDbType.NVarChar,50),
					new SqlParameter("@Tr_Money", SqlDbType.Decimal,9),
					new SqlParameter("@Tr_State", SqlDbType.Int,4),
					new SqlParameter("@TradeName", SqlDbType.NVarChar,100),
					new SqlParameter("@TradeNo", SqlDbType.NVarChar,50),
					new SqlParameter("@TradeStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@Created_Date", SqlDbType.DateTime),
					new SqlParameter("@Update_Date", SqlDbType.DateTime),
					new SqlParameter("@OrderNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@TR_ID", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.User_ID;
            parameters[1].Value = model.Tr_code;
            parameters[2].Value = model.Tr_Money;
            parameters[3].Value = model.Tr_State;
            parameters[4].Value = model.TradeName;
            parameters[5].Value = model.TradeNo;
            parameters[6].Value = model.TradeStatus;
            parameters[7].Value = model.Created_Date;
            parameters[8].Value = model.Update_Date;
            parameters[9].Value = model.OrderNumber;
            parameters[10].Value = model.TR_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string TR_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Transaction_records ");
            strSql.Append(" where TR_ID=@TR_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@TR_ID", SqlDbType.NVarChar,50)			};
            parameters[0].Value = TR_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string TR_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Transaction_records ");
            strSql.Append(" where TR_ID in (" + TR_IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Transaction_records GetModel(string TR_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 TR_ID,User_ID,Tr_code,Tr_Money,Tr_State,TradeName,TradeNo,TradeStatus,Created_Date,Update_Date,OrderNumber from Transaction_records ");
            strSql.Append(" where TR_ID=@TR_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@TR_ID", SqlDbType.NVarChar,50)			};
            parameters[0].Value = TR_ID;

            Model.Transaction_records model = new Model.Transaction_records();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Transaction_records DataRowToModel(DataRow row)
        {
            Model.Transaction_records model = new Model.Transaction_records();
            if (row != null)
            {
                if (row["TR_ID"] != null)
                {
                    model.TR_ID = row["TR_ID"].ToString();
                }
                if (row["User_ID"] != null)
                {
                    model.User_ID = row["User_ID"].ToString();
                }
                if (row["Tr_code"] != null)
                {
                    model.Tr_code = row["Tr_code"].ToString();
                }
                if (row["Tr_Money"] != null && row["Tr_Money"].ToString() != "")
                {
                    model.Tr_Money = decimal.Parse(row["Tr_Money"].ToString());
                }
                if (row["Tr_State"] != null && row["Tr_State"].ToString() != "")
                {
                    model.Tr_State = int.Parse(row["Tr_State"].ToString());
                }
                if (row["TradeName"] != null)
                {
                    model.TradeName = row["TradeName"].ToString();
                }
                if (row["TradeNo"] != null)
                {
                    model.TradeNo = row["TradeNo"].ToString();
                }
                if (row["TradeStatus"] != null)
                {
                    model.TradeStatus = row["TradeStatus"].ToString();
                }
                if (row["Created_Date"] != null && row["Created_Date"].ToString() != "")
                {
                    model.Created_Date = DateTime.Parse(row["Created_Date"].ToString());
                }
                if (row["Update_Date"] != null && row["Update_Date"].ToString() != "")
                {
                    model.Update_Date = DateTime.Parse(row["Update_Date"].ToString());
                }
                if (row["OrderNumber"] != null)
                {
                    model.OrderNumber = row["OrderNumber"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TR_ID,User_ID,Tr_code,Tr_Money,Tr_State,TradeName,TradeNo,TradeStatus,Created_Date,Update_Date,OrderNumber ");
            strSql.Append(" FROM Transaction_records ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" TR_ID,User_ID,Tr_code,Tr_Money,Tr_State,TradeName,TradeNo,TradeStatus,Created_Date,Update_Date,OrderNumber ");
            strSql.Append(" FROM Transaction_records ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Transaction_records ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.TR_ID desc");
            }
            strSql.Append(")AS Row, T.*  from Transaction_records T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Transaction_records";
            parameters[1].Value = "TR_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

